package org.ellwood.core.common.result;

import com.github.pagehelper.Page;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.ellwood.core.utils.PageUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: ellwood-boot-pro
 * @PackageName: org.ellwood.core.common.result
 * @ClassName: ApiPageResult
 * @Description: // 全局通用返回结构(分页)
 * @Author: Ellwood
 * @CreateDate: 2024/6/15 - 上午12:35
 * @Version: V1.0.0
 */
public class ApiPageResult<T> extends ApiResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    {
        code="0000";
        message="SUCCESS";
    }

    /**
     * 有参返回结果
     *
     * @param data 数据
     * @param <T> 返回实体
     * @return 返回结果
     */
    public static <T> ApiResult<PageResult<T>> success(List<T> data) {
        ApiResult<PageResult<T>> apiResult = new ApiPageResult<>();
        apiResult.data =
                (data != null) ? PageUtils.getPageResult(data) : PageUtils.getPageResult(new ArrayList<>());
        return apiResult;
    }

    public static <T> ApiResult<PageResult<T>> success(Page<T> page){
        return success(PageUtils.getPageResult(page));
    }

    /**
     * 有参返回结果
     *
     * @param data 数据
     * @param <T> 返回实体
     * @return 返回结果
     */
    public static <T> ApiResult<PageResult<T>> success(List<T> data, Object param) {
        ApiResult<PageResult<T>> apiResult = new ApiPageResult<>();
        apiResult.data =
                (data != null) ? PageUtils.getPageResult(data) : PageUtils.getPageResult(new ArrayList<>(), param);
        return apiResult;
    }

    public static <T> ApiPageResult<PageResult<T>> success(PageResult<T> data) {
        ApiPageResult<PageResult<T>> apiResult = new ApiPageResult<>();
        apiResult.data = (data != null) ? data : PageUtils.getPageResult(new ArrayList<>());
        return apiResult;
    }

}
